package restPROC;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import utilClasses.ListaPrevoznika;




import dao.daoKonekcija;
import dao.daoPermisije;
import dao.daoPrevoznik;

@Path("/unos")
public class Prevoznik {
	@POST
	@Path("/prevoznik")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("text/plain")
	public String unesiPrevoznika(@FormParam("naziv") String naziv,@FormParam("telefon") String telefon, @FormParam("webAdresa") String web,@FormParam("email") String email,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modprevoznik");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			System.out.print(naziv);
			daoPrevoznik.kreirajPrevoznika(naziv, telefon, email, web);
			
			
			return "true";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "false";
	}
	@POST
	@Path("/test")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("text/plain")
	public String test()
	{
		return "Bravo!!";
	}

	@POST
	@Path("/listaprevoznika")
	@Produces({"application/xml"})
	public ListaPrevoznika getPrevoznici()
	{
		ListaPrevoznika rez = new ListaPrevoznika();
		try {
			
			rez=daoPrevoznik.dajPrevoznike();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	
	@POST
	@Path("/brisanje")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("text/plain")
	public String deletePrevoznika(@FormParam("naziv") String naziv,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modprevoznik");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			System.out.print(naziv);
			daoPrevoznik.brisiPrevoznika(naziv);
			
			
			return "true";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "false";
	}
	
		@GET
		@Path("/dajImena")
		@Produces({"application/xml"})
		public ListaPrevoznika dajImena()
		{
			ListaPrevoznika rez=new ListaPrevoznika();
			try {
				
				rez=daoPrevoznik.dajImenaPrevoznika();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return rez;
		}	
	
}
